<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: 32104
  Date: 2024/6/14
  Time: 下午12:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>新增</title>
</head>
<body>
<%

        request.setCharacterEncoding("utf-8");
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        String id = request.getParameter("id");
        String name = request.getParameter("studentName");
        String age = request.getParameter("studentAge");
        String address = request.getParameter("studentAddress");
        String phone = request.getParameter("studentPhone");
        String sex = request.getParameter("studentSex");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String user = "root";
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String password = "root";
            connection = DriverManager.getConnection(url, user, password);
            String addSQL = "replace into user_info values (?,?,?,?,?,?);";
            preparedStatement = connection.prepareStatement(addSQL);
            preparedStatement.setInt(1, Integer.parseInt(id));
            preparedStatement.setString(2, name);
            preparedStatement.setInt(3, Integer.parseInt(age));
            preparedStatement.setString(4, address);
            preparedStatement.setString(5, phone);
            preparedStatement.setInt(6, Integer.parseInt(sex));
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
%>
<script>
    alert("新增成功")
    window.location.href = '../select/select01.jsp'
</script>
<%
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != preparedStatement) {
                    preparedStatement.close();
                }
                if (null != connection) {
                    connection.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
%>

</body>
</html>
